const mongoUtil = require("./mongo_util")
const collectionName = "student_score"
// const collectionsearchName = "indexInfo"

function addScore(score, callback) {
    var reg = new RegExp(score.SecondaryCollege);
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.find({
            'SecondaryCollege': reg,
            // 'Secondarystudent.studentList.student_Number': score.student_number
        }).toArray(function (err, result) {
            // console.log("result", result);
            // callback(err, result)
            if (err) {
                callback(err, result)
                return
            }
            if (result.length == 0) {
                mongoUtil.getCollection(collectionName).then(collection => {
                    collection.insertOne(score.dataList, function (err, res) {
                        callback(err, score.dataList)
                    });
                })
            } else {
                result.forEach(item => {
                    const res1 = item.Secondarystudent.filter(ite => {
                        const regClass = new RegExp(score.className)
                        return regClass.test(ite.className)
                    })
                    // console.log(res1.length,"cccccccccccccccccccccccccccccccccccccccc");
                    if (res1.length > 0) {
                        const res2 = res1[0].studentList.filter(it => {
                            return it.student_Number === score.student_Number
                        })
                        if (res2.length === 0) {
                            console.log('11111111');
                            res1[0].studentList.push(score.dataList.Secondarystudent[0].studentList[0])
                            // item.Secondarystudent.push(res1[0])
                            const whereStr = {
                                // 'SecondaryStudentList.scoreList.student_number': score.student_number,
                                'SecondaryCollege': score.SecondaryCollege,
                                // "SecondaryStudent.className": {
                                //     $ne: score.className
                                // },
                                // "SecondaryStudent.studentList.student_Number": {
                                //     $ne: score.student_number
                                // }
                            }
                            const updateStr = {
                                $set: {
                                    'Secondarystudent': item.Secondarystudent,
                                }
                            };
                            mongoUtil.getCollection(collectionName).then(collection => {
                                collection.updateOne(whereStr, updateStr, function (err, res) {

                                    callback(err, score.dataList)
                                });
                            })
                            return
                        }
                        const res3 = res2[0].schoolYearList.filter(schoolYear => {
                            return schoolYear.schoolYear === score.school_year
                        })
                        if (res3.length === 0) {
                            console.log('22222222');
                            res2[0].schoolYearList.push(score.dataList.Secondarystudent[0].studentList[0].schoolYearList[0])
                            // item.Secondarystudent.push(res1[0])
                            const whereStr = {
                                // 'SecondaryStudentList.scoreList.student_number': score.student_number,
                                'SecondaryCollege': score.SecondaryCollege,
                                // "SecondaryStudent.className": {
                                //     $ne: score.className
                                // },
                                // "SecondaryStudent.studentList.student_Number": {
                                //     $ne: score.student_number
                                // }
                            }
                            const updateStr = {
                                $set: {
                                    'Secondarystudent': item.Secondarystudent,
                                }
                            };
                            mongoUtil.getCollection(collectionName).then(collection => {
                                collection.updateOne(whereStr, updateStr, function (err, res) {

                                    callback(err, score.dataList)
                                });
                            })
                            return
                        }
                        const res4 = res3[0].score_item.filter(semester => {
                            return semester.semester === score.semester
                        })
                        if (res4.length === 0) {
                            console.log('33333333');
                            res3[0].score_item.push(score.dataList.Secondarystudent[0].studentList[0].schoolYearList[0].score_item[0])
                            // item.Secondarystudent.push(res1[0])
                            const whereStr = {
                                // 'SecondaryStudentList.scoreList.student_number': score.student_number,
                                'SecondaryCollege': score.SecondaryCollege,
                                // "SecondaryStudent.className": {
                                //     $ne: score.className
                                // },
                                // "SecondaryStudent.studentList.student_Number": {
                                //     $ne: score.student_number
                                // }
                            }
                            const updateStr = {
                                $set: {
                                    'Secondarystudent': item.Secondarystudent,
                                }
                            };
                            mongoUtil.getCollection(collectionName).then(collection => {
                                collection.updateOne(whereStr, updateStr, function (err, res) {

                                    callback(err, score.dataList)
                                });
                            })
                            return
                        }
                        console.log("888888888", res4);
                        // res3[0].score_item[0] 
                        res3[0].score_item.forEach((semester,index) => {
                            if (semester.semester === score.semester) {
                                res3[0].score_item[index] = score.dataList.Secondarystudent[0].studentList[0].schoolYearList[0].score_item[0]
                                // res3[0].score_item[0].scoreList = score.dataList.Secondarystudent[0].studentList[0].schoolYearList[0].score_item[0].scoreList

                                // item.Secondarystudent.push(res1[0])
                                // console.log("item.Secondarystudent==", item.Secondarystudent);
                                const whereStr = {
                                    // 'SecondaryStudentList.scoreList.student_number': score.student_number,
                                    'SecondaryCollege': score.SecondaryCollege,
                                    // "SecondaryStudent.className": {
                                    //     $ne: score.className
                                    // },
                                    // "SecondaryStudent.studentList.student_Number": {
                                    //     $ne: score.student_number
                                    // }
                                }
                                const updateStr = {
                                    $set: {
                                        'Secondarystudent': item.Secondarystudent,
                                    }
                                };
                                mongoUtil.getCollection(collectionName).then(collection => {
                                    collection.updateOne(whereStr, updateStr, function (err, res) {

                                        callback(err, score.dataList)
                                    });
                                })
                            }
                        })

                    } else {
                        console.log("99999999");
                        item.Secondarystudent.push(score.dataList.Secondarystudent[0])
                        const whereStr = {
                            // 'SecondaryStudentList.scoreList.student_number': score.student_number,
                            'SecondaryCollege': score.SecondaryCollege,
                            // "SecondaryStudent.className": {
                            //     $ne: score.className
                            // },
                            // "SecondaryStudent.studentList.student_Number": {
                            //     $ne: score.student_number
                            // }
                        }
                        const updateStr = {
                            $set: {
                                'Secondarystudent': item.Secondarystudent,
                            }
                        };
                        mongoUtil.getCollection(collectionName).then(collection => {
                            collection.updateOne(whereStr, updateStr, function (err, res) {

                                callback(err, score.dataList)
                            });
                        })
                    }
                })
            }

        })
    })
}
module.exports = {
    addScore,
}